package com.ty.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ty.mapper.SettleCaseMapper;
import com.ty.pojo.SettleCase;
import com.ty.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.security.InvalidParameterException;
import java.util.List;

@Service
public class SettleCaseServiceImpl implements SettleCaseService{
    @Autowired
    private SettleCaseMapper settleCaseMapper;

    @Override
    public void addSettleCase(SettleCase settleCase) {
        settleCase.setState(1);
        settleCaseMapper.insert(settleCase);
    }

    @Override
    public void updateSettleCase(SettleCase settleCase) {
        settleCaseMapper.updateById(settleCase);
    }

    @Override
    public void updateState(Integer id) {
        QueryWrapper<SettleCase> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        SettleCase settleCase = new SettleCase();
        settleCase.setState(0);
        settleCaseMapper.update(settleCase, queryWrapper);
    }

    @Override
    public PageResult getSettleCaseList(PageResult pageResult, SettleCase settleCase) {
        IPage<SettleCase> iPage = new Page<>(pageResult.getPageSize(), pageResult.getPageNum());
        QueryWrapper<SettleCase> queryWrapper = new QueryWrapper<>();
        boolean getName = StringUtils.hasLength(settleCase.getCaseName());
        boolean getType;
        if (settleCase.getCaseType() == null || "".equals(settleCase.getCaseType())){
            getType = false;
        }else {
            getType = true;
        }
        queryWrapper.and(Wrapper -> Wrapper.like(getName,"case_name", settleCase.getCaseName())
                .eq(getType,"case_type",settleCase.getCaseType())
                .eq("state", 1));
        iPage = settleCaseMapper.selectPage(iPage, queryWrapper);
        List<SettleCase> records = iPage.getRecords();
        long total = iPage.getTotal();
        return pageResult.setTotal(total).setRows(records);
    }
}
